﻿#region

using SixLabors.Fonts;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using Vin.Captcha.Tests;
using Vin.Tool.Core.JsonCategory;
using Color = SixLabors.ImageSharp.Color;

#endregion

Console.WriteLine("Hello Captcha");

Test test = new Test();
test.Name = "测试";
test.Dict ??= new Dictionary<string, string>();
test.Dict.Add("UserName", "1111");
test.Dict.Add("UserName2", "2222");

var str = test.ToCamelCaseJson();

var test2 = str.ToCamelCaseObject<Test>();


string GenerateRandomColor()
{
    var random = new Random();
    var color = string.Format("#{0:X6}", random.Next(0x1000000));
    return color;
}

var randomColor = GenerateRandomColor();

var color = Color.ParseHex("#FF0000");

int width = 100;
int height = 50;
int fontSize = 20;
var colors = new Color[]
{
    Color.Black, Color.Red, Color.Blue, Color.Green, Color.Yellow, Color.Purple, Color.Orange, Color.Gray, Color.Brown,
    Color.Pink, Color.Cyan, Color.Magenta
};
int angleMin = -70;
int angleMax = 70;
var random = new Random();

using var image = new Image<Rgba32>(width, height);

// 加载系统的字体
FontCollection fontCollection = new FontCollection();
fontCollection.AddSystemFonts();
FontFamily fontFamily = fontCollection.Families.First();
Font font = fontFamily.CreateFont(fontSize, FontStyle.Regular);
// 绘制文字
var code = new char[] { 'A', 'C', '5', '4' };
image.Mutate(ctx =>
{
    // 绘制背景
    ctx.Fill(Color.White);
    // 循环绘制文字，每个文字的位置都是随机的，颜色也是随机的，角度也是随机的
    for (int i = 0; i < code.Length; i++)
    {
        var angle = random.Next(angleMin, angleMax);
        var point = new PointF(i * fontSize + fontSize, height / 2 - fontSize / 2);
        var color = colors[random.Next(0, colors.Length)];
        Console.WriteLine($"angle: {angle}, point: {point}, color: {color}");
        var drawingOptions = new DrawingOptions
        {
            Transform = Matrix3x2Extensions.CreateRotationDegrees(angle, point)
        };
        ctx.DrawText(drawingOptions, code[i].ToString(), font, color, point);
    }

    // 绘制干扰线
    for (int i = 0; i < 10; i++)
    {
        var p1 = new PointF(random.Next(0, width), random.Next(0, height));
        var p2 = new PointF(random.Next(0, width), random.Next(0, height));
        ctx.DrawLine(colors[random.Next(0, colors.Length)], 1, p1, p2);
    }
});

image.Save("test.png");